<template>
  <el-form
    ref="userForm"
    :rules="rules"
    :model="userInfo"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:" prop="username">
      <el-input v-model="userInfo.username" style="width: 300px" />
    </el-form-item>
    <el-form-item label="密码:">
      <el-input
        v-model="userInfo.password"
        disabled
        type="password"
        style="width: 300px"
      />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="hSubmit">更新</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getuserByid, saveUserDetailById } from '@/api/user'
export default {
  data() {
    return {
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loaduserphoto()
  },
  methods: {
    // 确定按钮
    hSubmit() {
      // 兜底校验
      this.$refs.userForm.validate((valid) => {
        if (!valid) return
        this.loadUserDetailById()
      })
    },
    // 修改信息
    async loadUserDetailById() {
      try {
        const res = await saveUserDetailById(this.userInfo)
        console.log('saveUserDetailById', res)
        console.log(res)
        this.$message.success('用户名更新成功')
        // 返回上一级
        this.$router.back()
        // 保存数据
      } catch (err) {
        console.log('saveUserDetailById', err)
      }
    },
    // 获取个人信息
    async loaduserphoto() {
      try {
        const res = await getuserByid(this.$route.params.id)
        console.log('res', res)
        this.userInfo = res.data
        // 保存数据
      } catch (err) {
        console.log('error', err)
      }
    }
  }
}
</script>
